var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    average:0,
    active: 1,
    tabsList: [
      {
        value: 1,
        name: "奖励记录",
        isShow: true
      },
      {
        value: 2,
        name: "服务记录",
        isShow: true
      },
      {
        value: 3,
        name: "绩效考核记录",
        isShow: true
      }
    ],
    reward_type_array: [],
    rewardType: '',
    popupShow: false,
    detailList:[],
    listData:[],
    page: 1,
    finished: false,
    total: '', //用于直推人数
    isShowDirect: false,
    subsidyData: null, //老板补贴升级记录
    detailShow:false,
    type_card: 0,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getEntranceData();
  },
  show_detail(){
    this.setData({detailShow:true})
  },
  close_detail(){
    this.setData({detailShow:false})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getIncomeRecordsData();
  },
  // 获取头部数据
  getEntranceData() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.link-move.frontend.home.index'),
      data: {
        // t:1
      },
      success: ({data}) => {
        if(data.result) {
          let tabsList = this.data.tabsList;
          if(data.data.hide_my_team) { 
            tabsList[1].isShow = false
          }else {
            tabsList[1].isShow = true
          }

          if(!data.data.star_subsidy_disable) {
            tabsList.push({
              value: 4,
              name: `${data.data.boss_subsidy_name}升级记录`,
              isShow: true
            })
          }    
          if(data.data.average_dividend){
            tabsList.push({
              value:5,
              name:data.data.average_dividend,
              isShow: true
            })
          }
          this.setData({
            allData: data.data,
            reward_type_array : data.data.reward_type_array,
            rewardType: data.data.reward_type_array[0].value,
            tabsList: tabsList,
          })
          wx.setNavigationBarTitle({
            title: data.data.plugin_name,
          });
          this.getIncomeRecordsData();
        }else {
          wx.showToast({
            icon: 'none',
            title: data.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  // 获取奖励记录
  getIncomeRecordsData() {
    if (this.data.finished) return;
    let json = {};
    let url = '';
    if(this.data.active == 1) {
      url = 'plugin.link-move.frontend.home.get-reward';
      json = {
        page: this.data.page,
        // t: 1,
        reward_type: this.data.rewardType
      }
    } else if(this.data.active == 2) {
      url = 'plugin.link-move.frontend.home.my-team';
      json = {
        page: this.data.page,
        // t: 1,
      }
    } else if(this.data.active == 3) {
      url = 'plugin.link-move.frontend.home.help-task';
      json = {
        page: this.data.page,
        // t: 1,
      }
    } else if (this.data.active == 4) {
      url = "plugin.link-move.frontend.home.star-subsidy";
      json = {
        // page: this.data.page,
        // t: 1
      };
    }  else if (this.data.active == 5) {
      url = "plugin.link-move-average-dividend.frontend.index.getList";
      json = {
        page: this.data.page,
      };
    }
    app._getNetWork({
      url: app.getNetAddresss(url),
      data: json,
      success: (respone) => {
        if (respone.data.result !== 1) {
          wx.showToast({
            title: respone.data.msg,
          });
          return false;
        }
        if(this.data.active == 4) {
          let subsidyData  = respone.data.data;
          this.setData({
            subsidyData,
            type_card: subsidyData.see_dot_condition == 1 ? 0 : 1
          })
          return
        }
        let res = respone.data.data;
        let finished = false;
        let listData = [];
        let total = 1;
        if(this.data.active == 5){
          this.setData({average:res.average})
          if (res.list.current_page === res.list.last_page) {
            this.finished = true;
          }
          this.getLog();
          listData = this.data.listData.concat(res.list.data);
          total = res.list.total;
        }else{
          listData = this.data.listData.concat(res.data);
          if (res.data.length === 0 || res.data.length < res.per_page || res.current_page === res.last_page) {
            finished = true;
          }
          total =res.total;
        }
        
        this.setData({
          finished,
          listData,
          page: ++this.data.page,
          total,
          isShowDirect: true
        });
      }
    });
  },
  // 获取帮扶查看详情数据
  getHelpData() {

  },
  changeTab(row) {
    this.setData({
      active: row.detail.name,
      page: 1,
      finished: false,
      listData:[],
      isShowDirect: false
    })
    this.getIncomeRecordsData();
  },
  rewardEvent(row) {
    this.setData({
      rewardType: row.currentTarget.dataset.item.value,
      page: 1,
      finished: false,
      listData:[],
    })
    this.getIncomeRecordsData();
  },
  onClose() {
    this.setData({ popupShow: false });
  },
  showPopup(data) {
    app._postNetWork({
      url: app.getNetAddresss('plugin.link-move.frontend.home.help-detail'),
      data: {
        // t:1,
        thaw_id: data.currentTarget.dataset.id,
        help_type: data.currentTarget.dataset.help_type
      },
      success: ({data}) => {
        if(data.result) {
          this.setData({ 
            popupShow: true ,
            detailList: data.data
          });
        }else {
          wx.showToast({
            icon: 'none',
            title: data.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  switchTypeCard(ev) {
    this.setData({
      type_card: ev.currentTarget.dataset.type
    })
  },
  // searchScrollLower() {
    // if(this.data.i > 6) {
    //   console.log(this.data,'已全部加载');
    //   return
    // }
    // let newArr = this.data.detailList;
    // newArr.push({
    //   id: this.data.i
    // })
    // this.data.i = this.data.i + 1;
    // this.setData({
    //   detailList: newArr
    // })
  //   console.log(this.data,'lowerlowerlower');
  // }
  getLog(page=1){
    app._getNetWork({
      url:app.getNetAddresss('plugin.link-move-average-dividend.frontend.index.getLog'),
      data:{page},
      success:resdata=>{
        let {result,data,msg} = resdata.data;
        if(result){
          let dividend_list = [];
          if(page == 1){
            dividend_list = data.data;
          }else{
            dividend_list = this.data.dividend_list.concat(data.data);
          }
          this.setData({
            dividend_total:data.last_page,
            dividend_page:data.current_page,
            dividend_list
          })
        }else{
          app.tips(msg);
        }
      }
    })
  },
  show_dividend(){
    this.setData({detailShow:true});
  },
  bindscrolltolower(){
    if(this.data.dividend_page>=this.data.dividend_total){
      return;
    }
    let page = this.data.dividend_page + 1;
    this.getLog(page);
  },
})